package com.example.boot.rabbitmq.customer.listener;

import com.example.boot.rabbitmq.common.consant.MqConstant;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

/**
 * @author root
 * @date 2021-03-04 16:56
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class IndexListener {


    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue(value = MqConstant.QUEUE),
            exchange = @Exchange(value = MqConstant.EXCHANGE),
            key = MqConstant.ROUTING_KEY)
    })
    public void onMessage(Message message) {
        String msg = new String(message.getBody(), StandardCharsets.UTF_8);


        log.info("received Direct message : " + msg);
    }


    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue(value = "queue.dlx1"),
                    exchange = @Exchange(value = "exchange.dlx1"),
                    key = MqConstant.ROUTING_KEY)
    })
    @RabbitHandler
    public void onMessageDlx(Message message) {

        log.info("received Direct message : " + new String(message.getBody()));
    }

//    @RabbitListener(bindings = {
//            @QueueBinding(value = @Queue(value = "queue.dlx1"),
//                    exchange = @Exchange(value = "exchange.dlx1"),
//                    key = "com.order")
//    })
//    @RabbitHandler
//    public void onMessageDlxTopic(Message message) {
//
//        log.info("received Direct message : " + new String(message.getBody()));
//    }


    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue(value = "queue.dlx1"),
                    exchange = @Exchange(value = "exchange.dlx1"),
                    key = "com.#")
    })
    @RabbitHandler
    public void onMessageDlxTopic1(Message message) {

        log.info("received Direct message : " + new String(message.getBody()));
    }
}
